package com.irdstudio.efp.esb.service.impl.sx;

import com.irdstudio.efp.esb.service.client.impl.BD3022000901ESBBeanCreator;
import com.irdstudio.efp.esb.service.client.impl.BDESBBeanCreator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.irdstudio.efp.esb.common.client.resp.EsbRespSysHeadBean;
import com.irdstudio.efp.esb.common.constant.EsbBizEnums;
import com.irdstudio.efp.esb.common.constant.MsLoanConstant;
import com.irdstudio.efp.esb.service.bo.req.sx.CreditReq;
import com.irdstudio.efp.esb.service.bo.resp.sx.CreditResp;
import com.irdstudio.efp.esb.service.client.ESBClientFactory;
import com.irdstudio.efp.esb.service.client.impl.MSESBBeanCreator;
import com.irdstudio.efp.esb.service.facade.sx.CreditService;
import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.basic.framework.core.util.SpringContextUtils;

/**
 * 授信审批通知接口
  * @author heyingfeng
  * @since 2019年3月7日 下午3:10:08
  * @version 1.0
 */
@Service("creditService")
public class CreditServiceImpl implements CreditService, FrameworkService {

	private static Logger log = LoggerFactory.getLogger(CreditServiceImpl.class);

	// 发送请求
	@Override
	public CreditResp creditNotify(CreditReq creditReq) throws Exception {
		CreditResp creditResp = new CreditResp();
		EsbRespSysHeadBean esbRespSysHeadBean = new EsbRespSysHeadBean();
		try {
			esbRespSysHeadBean = ESBClientFactory.buildClient().withBody(creditReq).withTradeNo(MsLoanConstant.TRADE_NO_30220009)
					.withScene(MsLoanConstant.SCENE_01).withESBBeanFactory((BD3022000901ESBBeanCreator) SpringContextUtils.getBean("BD3022000901ESBBeanCreator")).create().sendAndReceive().getSysHead();
				if (MsLoanConstant.RetCodeEnum.SUCCESS.VALUE.equals(esbRespSysHeadBean.getRetInfArry().get(0).getRetCd()) ||
						"E1001".equals(esbRespSysHeadBean.getRetInfArry().get(0).getRetCd())) {
					log.info("授信审批结果消费接口调用，成功返回，开始进行后处理 ，请求参数： " + creditReq + ", 返回结果： " + creditResp);
					creditResp.setRetCd(esbRespSysHeadBean.getRetInfArry().get(0).getRetCd());
					return creditResp;
				}
//			}
			log.error("授信审批结果消费接口调用失败！,返回结果： " + creditResp);
			throw new ESBException();
		} catch (Exception e) {
			log.error("授信审批结果消费接口调用失败！"+e.getMessage());
			e.printStackTrace();
//			throw e;
		}
		return creditResp;
	}

}
